package user.zc.excel.data.info;

import user.zc.helper.ExcelAssistant;
import user.zc.excel.data.style.IStyle;
import user.zc.exception.ExcelExceptionEnum;
import lombok.SneakyThrows;

import java.lang.reflect.Field;

public class DataCell<T> implements ICell<Field>{

    private T item;
    private IStyle.StyleInfo style;

    public DataCell(T item, IStyle.StyleInfo style){
        this.item = item;
        this.style = style;
    }
    @Override
    @SneakyThrows
    public CellInfo parse(Field field) {
        field.setAccessible(true);
        ExcelAssistant excelAssistant =  field.getAnnotation(ExcelAssistant.class);
        ExcelExceptionEnum.OBJECT_NOT_EXIST.assertNotNull(excelAssistant);
        CellInfo cellInfo = new CellInfo();
        cellInfo.setData(field.get(item));
        cellInfo.setCellType(CellType.byClass(field.getType()).getValue());
        cellInfo.setStyleInfo(style);
        cellInfo.setRowSpanIfSameValue(excelAssistant.rowSpanIfSameValue());
        return cellInfo;
    }
}
